package com.google.shop.product.controller.qqlogin;

import com.google.shop.product.common.utils.JwtUtils;
import com.google.shop.product.common.utils.R;
import com.google.shop.product.entity.member.BuyerInfo;
import com.google.shop.product.repository.member.BuyerInfoRepository;
import com.google.shop.product.utils.HttpRequest;
import com.qq.connect.QQConnectException;
import com.qq.connect.api.OpenID;
import com.qq.connect.api.qzone.UserInfo;
import com.qq.connect.javabeans.qzone.UserInfoBean;
import com.qq.connect.oauth.Oauth;
import java.io.IOException;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin
/* loaded from: app.jar:BOOT-INF/classes/com/google/shop/product/controller/qqlogin/QqLoginController.class */
public class QqLoginController {
    private static final Logger log = LoggerFactory.getLogger(QqLoginController.class);
    @Autowired
    BuyerInfoRepository buyerInfoRepository;

    @RequestMapping({"verifyQqLoginCode"})
    public R verifyQqLoginCode(String code) {
        new R();
        try {
            String s = HttpRequest.sendGet("https://graph.qq.com/oauth2.0/token", "grant_type=authorization_code&client_id=101974998&client_secret=46aeffc0471926b9985ca1ab2cee6972&code=" + code + "&redirect_uri=https://www.liuzhuangzhuang1993.cn/api/qqlogin");
            log.info("verifyQqLoginCode接口，qq登录返回的所有的值：" + s);
            String accessToken = s.split(BeanFactory.FACTORY_BEAN_PREFIX)[0].split("=")[1];
            System.out.println("accessToken：" + accessToken);
            String openID = new OpenID(accessToken).getUserOpenID();
            UserInfoBean userInfoBean = new UserInfo(accessToken, openID).getUserInfo();
            if (userInfoBean.getRet() == 0) {
                String name = removeNonBmpUnicode(userInfoBean.getNickname());
                String imgUrl = userInfoBean.getAvatar().getAvatarURL100();
                HashMap<String, String> map = new HashMap<>();
                map.put("openId", openID);
                map.put("name", name);
                map.put("imgUrl", imgUrl);
                BuyerInfo byBuyerQqOpenid = this.buyerInfoRepository.findByBuyerQqOpenid(openID);
                if (byBuyerQqOpenid == null) {
                    BuyerInfo buyerInfo = new BuyerInfo();
                    buyerInfo.setBuyerQqOpenid(openID);
                    buyerInfo.setBuyerEmail(name);
                    buyerInfo.setBuyerQqImg(imgUrl);
                    byBuyerQqOpenid = (BuyerInfo) this.buyerInfoRepository.save(buyerInfo);
                }
                Object jwtToken = JwtUtils.getJwtToken(byBuyerQqOpenid.getBuyerId() + "", byBuyerQqOpenid.getBuyerEmail());
                HashMap<Object, Object> map2 = new HashMap<>();
                map2.put("jwtToken", jwtToken);
                return R.ok(map2);
            }
            log.info("很抱歉，我们没能正确获取到您的信息，原因是： " + userInfoBean.getMsg());
            System.out.println("很抱歉，我们没能正确获取到您的信息，原因是： " + userInfoBean.getMsg());
            return R.error("很抱歉，我们没能正确获取到您的信息，原因是： " + userInfoBean.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    @RequestMapping({"api/skipqq"})
    public void loginByQQ(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("text/html;charset=utf-8");
        try {
            response.sendRedirect(new Oauth().getAuthorizeURL(request));
            System.out.println("请求QQ登录,开始跳转...");
        } catch (QQConnectException | IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping({"api/qqlogin"})
    public void connection(HttpServletRequest request, HttpServletResponse response, String code) {
        try {
            response.sendRedirect("https://www.liuzhuangzhuang1993.cn?code=" + code);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String removeNonBmpUnicode(String str) {
        if (str == null) {
            return null;
        }
        String str2 = str.replaceAll("[^\\u0000-\\uFFFF]", "");
        if ("".equals(str2)) {
            str2 = "($ _ $)";
        }
        return str2;
    }
}
